/*************************************************************************
 *************************************************************************
 	 	 	 	 	 	 	M2SMA : DWGameAgent.cpp
 *************************************************************************
 *
 	 Copyright (C) 2011
 	 DW-2011, Steven Costiou, France
 *
 **************************************************************************
 **************************************************************************
 *
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/
 *
 **************************************************************************
 **************************************************************************/

#include "DWGameAgent.h"
#include <iostream>
#include <iomanip>
#include <cstdlib>


DWGameAgent::DWGameAgent(Engine* anEngine) : Agent(anEngine)
{
	addClassName("GAME_AGENT");

	initialize();
	initializeGame();
}

DWGameAgent::~DWGameAgent()
{

}

DWGameAgent::DWGameAgent(const DWGameAgent& aDWGameAgent)
{
	/// TODO
}

void DWGameAgent::selfCopyFrom(const DWGameAgent& anAgent, Engine* anEngine)
{
	/// TODO
}

bool DWGameAgent::isEqualTo(const DWGameAgent& anAgent) const
{
	/// TODO
	return false;
}

void DWGameAgent::initialize()
{

}

void DWGameAgent::initializeGame()
{
	Config cfg;


	// Read the file. If there is an error, report it and exit.
	try
	{
		cfg.readFile("/home/rstv/workspace/Darth-Wouf-2011/configuration/dw.cfg");
	}
	catch(const FileIOException &fioex)
	{
		std::cerr << "I/O error while reading file." << std::endl;
	}
	catch(const ParseException &pex)
	{

	}

	// Get the store name.
	try
	{
		string name = cfg.lookup("name");
		cout << "Store name: " << name << endl << endl;
	}
	catch(const SettingNotFoundException &nfex)
	{
		cerr << "No 'name' setting in configuration file." << endl;
	}

	const Setting& root = cfg.getRoot();

	// Output a list of all books in the inventory.
	try
	{
		const Setting &pommes = root["objects"]["pommes"];
		int count = pommes.getLength();

		cout << setw(30) << left << "ID" << "   "
				<< setw(6) << left << "X" << "  "
				<< "Y"
				<< endl;

		for(int i = 0; i < count; ++i)
		{
			const Setting &pomme = pommes[i];

			// Only output the record if all of the expected fields are present.
			string id;
			int x, y;

			if(!(pomme.lookupValue("id", id)
					&& pomme.lookupValue("x", x)
					&& pomme.lookupValue("y", y)))
				continue;

			cout << setw(30) << left << id << "  "
					<< setw(6) << left << x << "  "
					<< right << y << "  "
					<< endl;
		}
		cout << endl;
	}
	catch(const SettingNotFoundException &nfex)
	{
		// Ignore.
	}
}

void DWGameAgent::live()
{

}

